package siedler.GUI.gameElements;

import java.util.ArrayList;

import util.simpleIO.Out;

public class CornerList {
	
	private ArrayList<Corner> corners = new ArrayList<Corner>();	// list of all corners
	
	/**
	 * getCorners
	 * @return all corners
	 */
	public Corner[] getCorners(){
		return corners.toArray(new Corner[0]);
	}
	
	/**
	 * addCorner
	 * @param c
	 * @return 	Added:     'c', the corner that was added.
	 * 			Not Added: the existing, equivalent corner.
	 */
	public Corner addCorner(Corner c){
		
		if(corners.contains(c)){
			//Out.pl("> Contained ... " + c);
			return getCorner(c.fields);
		}
		
		//Out.pl("> New ... " + c);
		corners.add(c);
		return c;
	}
	
	/**
	 * getCorner - doesn't return all single field corners!
	 * Uses overwritten equals method.
	 * @param fields
	 * @return
	 */
	public Corner getCorner(ArrayList<Field> fields){
		Corner tmp = new Corner(fields, null, 0);
		
		for(Corner c : corners){
			if(c.equals(tmp))
				return c;
		}
		throw new RuntimeException("Corner does not exist!");
	}
	
	/**
	 * initAllConnectionsCoordinates
	 * Graphical initialization for all coordinates
	 */
	public void initAllCorners(){
		
		Out.pl("" + this + "");
		for(Corner c : corners){
			c.initGraphics();
		}
	}
	
	@Override
	public String toString(){
		return "CornerList: "+corners.size()+" corners";
	}
}
